//
//  OnLineProductTableViewCell.m
//  MyYiJianKangApp
//
//  Created by 张国文 on 16/4/29.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "OnLineProductTableViewCell.h"

@implementation OnLineProductTableViewCell

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self addMySubview];
    }
    return self;
}

// 布局
- (void)addMySubview
{
    // 产品图片
    self.onLineProductImageV = [[UIImageView alloc] initWithFrame:CGRectMake((375 - 307) / 2 * kAdjustParamater, 15 * kAdjustParamater, 307 * kAdjustParamater, 202 * kAdjustParamater)];
    self.onLineProductImageV.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    self.onLineProductImageV.layer.cornerRadius = 4 * kAdjustParamater;
    self.onLineProductImageV.layer.masksToBounds = YES;
    [self.contentView addSubview:_onLineProductImageV];
    
    // 产品介绍
    self.onLineProductTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.onLineProductImageV.bottom + 20 * kAdjustParamater, kScreenWidth, 20 * kAdjustParamater)];
    //  self.onLineProductTitleLabel.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    self.onLineProductTitleLabel.font = [UIFont systemFontOfSize:k18Big];
    self.onLineProductTitleLabel.textColor = [UIColor blackColor];
    self.onLineProductTitleLabel.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:_onLineProductTitleLabel];
    
    // 介绍下面的灰色条
    // 原来的
    //    UILabel *grayLineOnProductTitleLable = [[UILabel alloc] initWithFrame:CGRectMake(0, self.onLineProductTitleLabel.bottom + 15  * kAdjustParamater, kScreenWidth, 1.5 * kAdjustParamater)];
    UILabel *grayLineOnProductTitleLable = [[UILabel alloc] initWithFrame:CGRectMake(0, self.onLineProductTitleLabel.bottom + 5  * kAdjustParamater, kScreenWidth, 1.5 * kAdjustParamater)];
    grayLineOnProductTitleLable.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.1];
    [self.contentView addSubview:grayLineOnProductTitleLable];
    
    // 产品介绍底部灰色背景
    self.productContontBackGroundGrayView = [[UIView alloc] initWithFrame:CGRectMake(20 * kAdjustParamater, grayLineOnProductTitleLable.bottom + 17 * kAdjustParamater, 335 * kAdjustParamater, 60 * kAdjustParamater)];
    self.productContontBackGroundGrayView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.08];
    self.productContontBackGroundGrayView.layer.cornerRadius = 5 * kAdjustParamater;
    [self.contentView addSubview:_productContontBackGroundGrayView];
    
    // 产品介绍label
    self.onLineProductContentLabel = [[UILabel alloc] initWithFrame:CGRectMake(13 * kAdjustParamater, 15 * kAdjustParamater, 309 * kAdjustParamater, 50 * kAdjustParamater)];
    //  self.onLineProductContentLabel.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:0.1];
    self.onLineProductContentLabel.numberOfLines = 0;
    self.onLineProductContentLabel.textColor = [UIColor blackColor];
    self.onLineProductContentLabel.font = [UIFont systemFontOfSize:k17Big];
    [self.productContontBackGroundGrayView addSubview:_onLineProductContentLabel];
    
    // 产品介绍底部的灰色框
    self.grayLineOnBackGroundGrayView = [[UILabel alloc] initWithFrame:CGRectMake(0, self.productContontBackGroundGrayView.bottom + 18 * kAdjustParamater, kScreenWidth, 0.15 * kAdjustParamater)];
    self.grayLineOnBackGroundGrayView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    [self.contentView addSubview:_grayLineOnBackGroundGrayView];
    
    // 原价
    // 原来的
    //    self.onLineProductOriginPriceLabel = [[UILabel alloc] initWithFrame:CGRectMake(40 * kAdjustParamater, self.grayLineOnBackGroundGrayView.bottom + 33 * kAdjustParamater, 160 * kAdjustParamater, 20 * kAdjustParamater)];
    self.onLineProductOriginPriceLabel = [[UILabel alloc] initWithFrame:CGRectMake(40 * kAdjustParamater, self.grayLineOnBackGroundGrayView.bottom + 17 * kAdjustParamater, 160 * kAdjustParamater, 20 * kAdjustParamater)];
    //   self.onLineProductOriginPriceLabel.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    self.onLineProductOriginPriceLabel.text = @"原   价：";
    self.onLineProductOriginPriceLabel.textColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    self.onLineProductOriginPriceLabel.font = [UIFont systemFontOfSize:k18Big];
    self.onLineProductOriginPriceLabel.hidden = YES;
    self.onLineProductOriginPriceLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:_onLineProductOriginPriceLabel];
    
    // 活动价
    self.onLineProductActivitypriceLabel = [[UILabel alloc] initWithFrame:CGRectMake(40 * kAdjustParamater, self.onLineProductOriginPriceLabel.bottom + 27 * kAdjustParamater, 260 * kAdjustParamater, 20 * kAdjustParamater)];
    //  self.onLineProductActivitypriceLabel.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    self.onLineProductActivitypriceLabel.textColor = [UIColor redColor];
    self.onLineProductActivitypriceLabel.font  = [UIFont systemFontOfSize:22 * kAdjustParamater];
    self.onLineProductActivitypriceLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:_onLineProductActivitypriceLabel];
    
    // 原价上面的灰色条
    self.lineOnOriginPrice = [[UILabel alloc] initWithFrame:CGRectMake(self.onLineProductOriginPriceLabel.left - 15 * kAdjustParamater, self.onLineProductOriginPriceLabel.top + 9 * kAdjustParamater, 175 * kAdjustParamater, 1 * kAdjustParamater)];
    self.lineOnOriginPrice.hidden = YES;
    self.lineOnOriginPrice.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    [self.contentView addSubview:_lineOnOriginPrice];
    
    // 加入订单
    self.joinDingDanButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.joinDingDanButton.frame = CGRectMake((375 - 115 ) * kAdjustParamater, self.grayLineOnBackGroundGrayView.bottom + 33 * kAdjustParamater, 95 * kAdjustParamater, 25 * kAdjustParamater);
    [self.joinDingDanButton setTitle:@"加入订单" forState:UIControlStateNormal];
    [self.joinDingDanButton setTitleColor:kYellowColor forState:UIControlStateNormal];
    self.joinDingDanButton.titleLabel.font = [UIFont systemFontOfSize:k16Big];
    self.joinDingDanButton.layer.cornerRadius = 4 * kAdjustParamater;
    self.joinDingDanButton.layer.borderColor = [kYellowColor CGColor];
    self.joinDingDanButton.layer.borderWidth = 1 * kAdjustParamater;
    [self.contentView addSubview:_joinDingDanButton];
    
    self.joinDingDanButton.hidden = YES;
    // 立即购买
    self.buyNowButton = [UIButton buttonWithType:UIButtonTypeSystem];
    //    self.buyNowButton.frame = CGRectMake(self.joinDingDanButton.left, self.joinDingDanButton.bottom + 20 * kAdjustParamater, 95 * kAdjustParamater, 25 * kAdjustParamater);
    self.buyNowButton.frame = CGRectMake(self.joinDingDanButton.left, self.onLineProductOriginPriceLabel.bottom, 95 * kAdjustParamater, 25 * kAdjustParamater);
    [self.buyNowButton setTitle:@"立即购买" forState:UIControlStateNormal];
    [self.buyNowButton setTitleColor:kYellowColor forState:UIControlStateNormal];
    self.buyNowButton.titleLabel.font = [UIFont systemFontOfSize:k16Big];
    self.buyNowButton.layer.cornerRadius = 4 * kAdjustParamater;
    self.buyNowButton.layer.borderColor = [kYellowColor CGColor];
    self.buyNowButton.layer.borderWidth = 1 * kAdjustParamater;
    [self.contentView addSubview:_buyNowButton];
    
    
    // 底部灰色条
    self.grayLineOnCellBottom = [[UILabel alloc] initWithFrame:CGRectMake(0, self.buyNowButton.bottom + 28 * kAdjustParamater, kScreenWidth, 10 * kAdjustParamater)];
    self.grayLineOnCellBottom.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.05];
    [self.contentView addSubview:_grayLineOnCellBottom];
    
    
    
    
}


- (void)setModel:(OnLineProductModel *)model
{
    // 图像
    NSString *imageUrl = [model.advertis_img_url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    [self.onLineProductImageV sd_setImageWithURL:[NSURL URLWithString:imageUrl] placeholderImage:[UIImage imageNamed:@""]];
    // 标题
    self.onLineProductTitleLabel.text = model.short_description_title;
    
    // 介绍
    if (model.short_description.length == 0) {
        self.onLineProductContentLabel.text = @"简短内容介绍";
    } else {
        self.onLineProductContentLabel.text = model.short_description;
    }
    
    // 设置间距
    [self.onLineProductContentLabel setAttributedText:[[NetStatusHandle shareHandle] setHangJianJvOfLabel:self.onLineProductContentLabel.text lineHeight:8]];
    [self.onLineProductContentLabel sizeToFit];
    
    // 调整灰色背景的高度
    self.productContontBackGroundGrayView.frame = CGRectMake(self.productContontBackGroundGrayView.frame.origin.x, self.productContontBackGroundGrayView.frame.origin.y, self.productContontBackGroundGrayView.frame.size.width, self.onLineProductContentLabel.bottom + 15 * kAdjustParamater);
    // 调整灰色背景下的灰色条的高度
    self.grayLineOnBackGroundGrayView.frame = CGRectMake(0, self.productContontBackGroundGrayView.bottom + 18 * kAdjustParamater, kScreenWidth, self.grayLineOnBackGroundGrayView.frame.size.height);
    // 调整原价的高度
    //  NSLog(@"原价:%@", model.price);
    self.onLineProductOriginPriceLabel.text = [NSString stringWithFormat:@"原    价：￥%@",model.price];
    self.onLineProductOriginPriceLabel.frame = CGRectMake(self.onLineProductOriginPriceLabel.frame.origin.x, self.grayLineOnBackGroundGrayView.bottom + 17 * kAdjustParamater, self.onLineProductOriginPriceLabel.frame.size.width, self.onLineProductOriginPriceLabel.frame.size.height);
    // 调整原价上方的横条的高度
    self.lineOnOriginPrice.frame = CGRectMake(self.lineOnOriginPrice.frame.origin.x, self.onLineProductOriginPriceLabel.top + 9 * kAdjustParamater, self.lineOnOriginPrice.frame.size.width, self.lineOnOriginPrice.frame.size.height);
    
    // 调整活动价的高度
    self.onLineProductActivitypriceLabel.text = [NSString stringWithFormat:@"活动价：￥%@", model.promotion_price];
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:self.onLineProductActivitypriceLabel.text];
    [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:k18Big] range:NSMakeRange(0, 4)];
    [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:21 * kAdjustParamater] range:NSMakeRange(4, self.onLineProductActivitypriceLabel.text.length - 4)];
    [self.onLineProductActivitypriceLabel setAttributedText:str];
    self.onLineProductActivitypriceLabel.frame = CGRectMake(self.onLineProductOriginPriceLabel.left, self.onLineProductOriginPriceLabel.bottom + 27 * kAdjustParamater, self.onLineProductActivitypriceLabel.frame.size.width, self.onLineProductActivitypriceLabel.frame.size.height);
    
    // 调整加入订单的高度
    self.joinDingDanButton.frame = CGRectMake(self.joinDingDanButton.frame.origin.x, self.grayLineOnBackGroundGrayView.bottom + 33 * kAdjustParamater, self.joinDingDanButton.frame.size.width, self.joinDingDanButton.frame.size.height);
    // 调整立即购买的高度
    self.buyNowButton.frame = CGRectMake(self.joinDingDanButton.left, self.onLineProductOriginPriceLabel.bottom, self.buyNowButton.frame.size.width, self.buyNowButton.frame.size.height);
    
    
    
    // 调整底部灰色条的高度
    self.grayLineOnCellBottom.frame = CGRectMake(0, self.onLineProductActivitypriceLabel.bottom + 28 * kAdjustParamater, kScreenWidth, self.grayLineOnCellBottom.frame.size.height);
    
    if ([model.is_promotion isEqualToString:@"1"]) {
        // 说明促销
        self.lineOnOriginPrice.hidden = NO;
        self.onLineProductOriginPriceLabel.hidden = NO;
        self.onLineProductActivitypriceLabel.hidden = NO;
    } else {
        // 说明不促销
        self.lineOnOriginPrice.hidden = YES;
        self.onLineProductOriginPriceLabel.hidden = NO;
        self.onLineProductActivitypriceLabel.hidden = YES;
    }
    
}






@end
